#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <numeric>
using namespace std;

class Main {
	const int SIZE = 30000;
	vector<int> father, dist, size;
	stack<int> stk;
	int t, ans;
	int findr(int x)
	{
		if (x != father[x]) {
			int t = father[x];
			father[x] = findr(father[x]);
			dist[x] += dist[t];
		}
		return father[x];
	}
	int finds(int x)
	{
		while (x != father[x]) {
			stk.push(x);
			x = father[x];
		}

		int root = x;
		while (!stk.empty()) {
			int t = stk.top();
			stk.pop();

			father[t] = x;
			dist[t] += dist[root];
                        root = t;
		}
		return x;
	}
	void unite(int x, int y)
	{
		int fx = finds(x), fy = finds(y);
		if (fx != fy) {
			father[fx] = fy;
			dist[fx] = size[fy];
			size[fy] += size[fx];
		}
	}
	int query(int x, int y)
	{
		int fx = finds(x), fy = finds(y);
		if (fx == fy)
			return abs(dist[x] - dist[y]) - 1;
		else
			return -1;
	}

    public:
	Main() : father(SIZE + 1), dist(SIZE + 1, 0), size(SIZE + 1, 1)
	{
		iota(father.begin(), father.end(), 0);
	}

	void run()
	{
		char op;
		int t, l, r;
		cin >> t;
		while (t--) {
			cin >> op >> l >> r;
			if (op == 'M') {
				unite(l, r);
			} else {
				cout << query(l, r) << endl;
			}
		}
	}
};

int main()
{
	Main app;
	app.run();
	return 0;
}